<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>日记</title>
	<link rel="stylesheet" href="bootstrap.css">
	<link href="dist/summernote.css" rel="stylesheet"/>
	<link rel="stylesheet" href="../jQueryMsg/css/message.css">
	<script src="../assets/js/core/jquery.3.2.1.min.js"></script>
	<script src="bootstrap.js"></script>
	<script src="dist/summernote.js"></script>
	<script src="dist/lang/summernote-zh-CN.js"></script>    <!-- 中文-->
	<script src="../jQueryMsg/js/message.js"></script>

<script>
	var token;
    $(function () {
        $('#summernote').summernote({
			height:'400',
            focus: true,
            maxHeight: null,
            minHeight: null,
            toolbar: [
                // [groupName, [list of button]]
                ['style', ['bold', 'italic', 'underline', 'clear']],
                ['font', ['strikethrough', 'superscript', 'subscript']],
                ['fontsize', ['fontsize']],
                ['color', ['color']],
                ['para', ['ul', 'ol', 'paragraph']],
                ['height', ['height']]
            ]
		});
        token = sessionStorage.getItem('token')
    });
</script>
</head>

<body>
<div class="container">
	<div class="row" style="padding: 10px">
		<div class="col-md-10 col-md-offset-1">
			<h1>写日记</h1>
			<form>
				<div class="form-group form-inline">
					<label class="control-label" for="diaryTitle">日记标题</label>
					<input type="text" class="form-control form-control-sm" id="diaryTitle" style="width: 50%;" autocomplete="off">
				</div>
				<div class="form-group form-inline">
					<label class="control-label" for="teacher">审阅老师</label>
					<select id="teacher" class="form-control" style="width: 50%">
					</select>
				</div>
				<div id="diaryDay" class="form-group">
					<div id="summernote"></div>
				</div>
				<div style="float: right">
					<button onclick="back()" type="button" class="btn btn-default">返回</button>
					<button id="addBtn" type="button" class="btn btn-primary">提交日记</button>
				</div>
			</form>
		</div>
	</div>
</div>
<script>
    //回显数据
    var diaryId;
    $(function () {
        teacherSelect()
        diaryId = GetQueryString('diaryId')
		if (diaryId != 'null') {
            var param = {
                diaryId:diaryId
            }
            $.ajax({
                url: 'http://localhost:8080/oa/t-diary/listDiaryById',
                type: "GET",
                data: param,
                dataType: "json",
                headers: {
                    token: "" + token
                },
                success: function (data) {
                    if (data.code == 200){
                        $('#diaryTitle').val(data.data.diaryTitle)
                        $('#summernote').summernote('code',decodeURIComponent(data.data.diaryText))
                        $.ajax({
                            url: 'http://localhost:8080/oa/t-user/listTeacher',
                            type: "GET",
                            dataType: "json",
                            headers: {
                                token: "" + token
                            },
                            success: function (data) {
                                var data1 = data.data
                                $('#teacher').empty();
                                var html = "";
                                html += '<option value="">请选择</option>\n'
                                for (var i = 0; i < data1.length; i++) {
                                    if (data.data[i].userName == data1[i].userName) {
                                        html += '<option selected="selected" value="'+data1[i].id+'">'+data1[i].userName+'</option>\n'
                                    }else {
                                        html += '<option value="'+data1[i].id+'">'+data1[i].userName+'</option>\n'
                                    }
                                }
                                $('#teacher').append(html)
                            }
                        })
                    }else if (data.code == 401) {
                        window.location.href = 'login.html'
                    }else {
                        $.message({
                            message:data.msg,
                            type:'error'
                        });
                    }
                }
            })
		}
    })
    $('#addBtn').unbind("click").on('click',function () {
        if ($('#diaryTitle').val().replace(/\s+/g, "") == "") {
            $.message({
                message:'请输入标题',
                type:'warning'
            });
            return;
		}
        if ($('#teacher').val()=="") {
            $.message({
                message:'请选择角色',
                type:'warning'
            });
            return;
        }
        if ($($("#summernote").summernote("code")).text().replace(/\s+/g, "") == ""){
            $.message({
                message:'日记还没写',
                type:'warning'
            });
            return;
		}
        if (diaryId != 'null') {
            var param = {
                diaryId:diaryId,
				teacherId:$('#teacher').val(),
                diaryTitle:$('#diaryTitle').val(),
                diaryText:encodeURIComponent($('#summernote').summernote('code'))
            }
		}else {
            var param = {
                teacherId:$('#teacher').val(),
                diaryTitle:$('#diaryTitle').val(),
                diaryText:encodeURIComponent($('#summernote').summernote('code'))
            }
		}
        $.ajax({
            url: 'http://localhost:8080/oa/t-diary/addDiary',
            type: "POST",
            data: JSON.stringify(param),
            dataType: "json",
            headers: {
                token: "" + token
            },
            contentType: 'application/json; charset=UTF-8',
            success: function (data) {
                if (data.code == 200){
                    $.message(data.msg);
                    back()
                }else if (data.code == 401) {
                    window.location.href = 'login.html'
                }else {
                    $.message({
                        message:data.msg,
                        type:'error'
                    });
                }
                $('#table').bootstrapTable('refresh');
                $('#addModel').modal('hide')
            }
        })
    })

    function GetQueryString(name)
    {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }
	function back() {
        window.location.href = '../submission.html'
    }
    function teacherSelect() {
        $.ajax({
            url: 'http://localhost:8080/oa/t-user/listTeacher',
            type: "GET",
            dataType: "json",
            headers: {
                token: "" + token
            },
            success: function (data) {
                var data = data.data
                $('#teacher').empty();
                var html = "";
                html += '<option value="">请选择</option>\n'
                for (var i = 0; i < data.length; i++) {
                    html += '<option value="'+data[i].id+'">'+data[i].userName+'</option>\n'
                }
                $('#teacher').append(html)
            }
        })
    }
</script>
</body>
</html>
